home *** CD-ROM | disk | FTP | other *** search
/ PC Media 7 / PC MEDIA CD07.iso / share / prog / cm / cmset.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1994-09-06  |  2.4 KB  |  102 lines

  1. // CmSet.cpp
  2. // -----------------------------------------------------------------
  3. // Compendium - C++ Container Class Library
  4. // Copyright (C) 1992-1994, Glenn M. Poorman, All rights reserved
  5. // -----------------------------------------------------------------
  6. // Set implementation.
  7. // -----------------------------------------------------------------
  8.  
  9. #include <cm/include/cmset.h>
  10.  
  11.  
  12. // "CmSet" is the default set constructor.
  13. //
  14. CmSet::CmSet(unsigned sz) : CmHashTable(sz)
  15. {}
  16.  
  17.  
  18. // "CmSet" is the set copy constructor.
  19. //
  20. CmSet::CmSet(const CmSet& S) : CmHashTable(S)
  21. {}
  22.  
  23.  
  24. // "=" assignment operator copies the contents of the specified set
  25. // into this set.
  26. //
  27. CmSet& CmSet::operator=(const CmSet& S)
  28. {
  29.   return (CmSet&) CmHashTable::operator=(S);
  30. }
  31.  
  32.  
  33. // "-" difference operator returns a set containing those objects
  34. // appearing in this set and the input set but not in both sets.
  35. //
  36. CmSet CmSet::operator-(const CmSet& set)
  37. {
  38.   CmSet out;
  39.   out.ownsObjects(FALSE);
  40.  
  41.   CmHashTableIterator iterator(*this);
  42.   Bool                success = TRUE;
  43.   while (!iterator.done() && success)
  44.   {
  45.     CmObject* pObj = iterator.next();
  46.     if (!set.lookup(pObj)) success = out.add(pObj);
  47.   }
  48.  
  49.   CmHashTableIterator iterator2(set);
  50.   while (!iterator2.done() && success)
  51.   {
  52.     CmObject* pObj = iterator2.next();
  53.     if (!lookup(pObj)) success = out.add(pObj);
  54.   }
  55.   return out;
  56. }
  57.  
  58.  
  59. // "&" intersection operator returns a set containing only those
  60. // objects appearing in this set and the input set.
  61. //
  62. CmSet CmSet::operator&(const CmSet& set)
  63. {
  64.   CmSet out;
  65.   out.ownsObjects(FALSE);
  66.  
  67.   CmHashTableIterator iterator(*this);
  68.   Bool                success = TRUE;
  69.   while (!iterator.done() && success)
  70.   {
  71.     CmObject *pObj = iterator.next();
  72.     if (set.lookup(pObj)) success = out.add(pObj);
  73.   }
  74.   return out;
  75. }
  76.  
  77.  
  78. // "|" union operator returns a set containing those objects
  79. // appearing in this set, the input set, or both sets.
  80. //
  81. CmSet CmSet::operator|(const CmSet& set)
  82. {
  83.   CmSet out;
  84.   out.ownsObjects(FALSE);
  85.  
  86.   CmHashTableIterator iterator(*this);
  87.   while (!iterator.done()) out.add(iterator.next());
  88.  
  89.   CmHashTableIterator iterator2(set);
  90.   while (!iterator2.done()) out.add(iterator2.next());
  91.   return out;
  92. }
  93.  
  94.  
  95. // "add" adds a new object to the set provided it is not already
  96. // contained in the set.
  97. //
  98. Bool CmSet::add(CmObject* pObj)
  99. {
  100.   return (contains(pObj)) ? FALSE : CmHashTable::add(pObj);
  101. }
  102.